<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>worker-需使用localhost启动</title>
</head>
<body>

<button onclick="sendToWorker()">点击通信</button> 
 
 
<script>
var worker1 = new Worker("./worker1/index.js");;
 
function sendToWorker() {
    var num = parseInt(100 * Math.random());
    console.log('num1', num);
    worker1.postMessage(num);//将num发送给后台
}

worker1.onmessage = function(event) {
    console.log('num2', event.data);//接收来自 worker1.js 的数据
};
</script>
</body>
</html>